home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / manage / snmp / cmu-snmp1.0 / apps / snmpnetstat / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-20  |  6.2 KB  |  289 lines

  1. /***********************************************************
  2.     Copyright 1989 by Carnegie Mellon University
  3.  
  4.                       All Rights Reserved
  5.  
  6. Permission to use, copy, modify, and distribute this software and its 
  7. documentation for any purpose and without fee is hereby granted, 
  8. provided that the above copyright notice appear in all copies and that
  9. both that copyright notice and this permission notice appear in 
  10. supporting documentation, and that the name of CMU not be
  11. used in advertising or publicity pertaining to distribution of the
  12. software without specific, written prior permission.  
  13.  
  14. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  15. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  16. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  17. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  18. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  19. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  20. SOFTWARE.
  21. ******************************************************************/
  22. /*
  23.  * Copyright (c) 1983,1988 Regents of the University of California.
  24.  * All rights reserved.
  25.  *
  26.  * Redistribution and use in source and binary forms are permitted
  27.  * provided that this notice is preserved and that due credit is given
  28.  * to the University of California at Berkeley. The name of the University
  29.  * may not be used to endorse or promote products derived from this
  30.  * software without specific prior written permission. This software
  31.  * is provided ``as is'' without express or implied warranty.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif not lint
  39.  
  40. #include <sys/types.h>
  41. #include <sys/param.h>
  42.  
  43. #include <sys/socket.h>
  44. #include <sys/time.h>
  45.  
  46. #include <ctype.h>
  47. #include <errno.h>
  48. #include <netdb.h>
  49. #include <stdio.h>
  50. #include <netinet/in.h>
  51. #include "asn1.h"
  52. #include "snmp.h"
  53. #include "snmp_api.h"
  54.  
  55. /* internet protocols */
  56. extern    int protopr();
  57. extern    int tcp_stats(), udp_stats(), ip_stats(), icmp_stats();
  58.  
  59. #define NULLPROTOX    ((struct protox *) 0)
  60. struct protox {
  61.     u_char    pr_wanted;        /* 1 if wanted, 0 otherwise */
  62.     int    (*pr_cblocks)();    /* control blocks printing routine */
  63.     int    (*pr_stats)();        /* statistics printing routine */
  64.     char    *pr_name;        /* well-known name */
  65. } protox[] = {
  66.     { 1,    protopr,    tcp_stats,    "tcp" },
  67.     { 1,    0,        udp_stats,    "udp" },
  68.     { 1,    0,        ip_stats,    "ip" },
  69.     { 1,    0,        icmp_stats,    "icmp" },
  70.     { 0,    0,        0,        0 }
  71. };
  72.  
  73. int    aflag;
  74. int    iflag;
  75. int    nflag;
  76. int    pflag;
  77. int    rflag;
  78. int    sflag;
  79. int    interval;
  80. char    *interface;
  81. int    unit;
  82. char    usage[] = "host community [ -ainrs ] [-p proto] [-I interface] [ interval ]";
  83.  
  84. int debug = 0;
  85.  
  86.  
  87. extern    char *malloc();
  88.  
  89. struct snmp_session *Session;
  90. int snmp_dump_packet = 0;
  91. int print_errors = 0;
  92.  
  93. main(argc, argv)
  94.     int argc;
  95.     char *argv[];
  96. {
  97.     char *cp, *name;
  98.     char *host;
  99.     register struct protoent *p;
  100.     register struct protox *tp;    /* for printing cblocks & stats */
  101.     struct protox *name2protox();    /* for -p */
  102.     char *community;
  103.     struct snmp_session session;
  104.     
  105.     name = argv[0];
  106.     argc--, argv++;
  107.     if (argc--)
  108.         host = *argv++;
  109.     if (argc--)
  110.         community = *argv++;
  111.     else
  112.         goto use;
  113.       while (argc > 0 && **argv == '-') {
  114.         for (cp = &argv[0][1]; *cp; cp++)
  115.         switch(*cp) {
  116.  
  117.         case 'a':
  118.             aflag++;
  119.             break;
  120.  
  121.         case 'i':
  122.             iflag++;
  123.             break;
  124.  
  125.         case 'n':
  126.             nflag++;
  127.             break;
  128.  
  129.         case 'r':
  130.             rflag++;
  131.             break;
  132.  
  133.         case 's':
  134.             sflag++;
  135.             break;
  136.  
  137.         case 'p':
  138.             argv++;
  139.             argc--;
  140.             if (argc == 0)
  141.                 goto use;
  142.             if ((tp = name2protox(*argv)) == NULLPROTOX) {
  143.                 fprintf(stderr, "%s: unknown or uninstrumented protocol\n",
  144.                     *argv);
  145.                 exit(10);
  146.             }
  147.             pflag++;
  148.             break;
  149.  
  150.         case 'I':
  151.             iflag++;
  152.             if (*(interface = cp + 1) == 0) {
  153.                 if ((interface = argv[1]) == 0)
  154.                     break;
  155.                 argv++;
  156.                 argc--;
  157.             }
  158.             for (cp = interface; isalpha(*cp); cp++)
  159.                 ;
  160.             unit = atoi(cp);
  161.             break;
  162.  
  163.         default:
  164. use:
  165.             printf("usage: %s %s\n", name, usage);
  166.             exit(1);
  167.         }
  168.         argv++, argc--;
  169.     }
  170.     if (argc > 0 && isdigit(argv[0][0])) {
  171.         interval = atoi(argv[0]);
  172.         if (interval <= 0)
  173.             goto use;
  174.         argv++, argc--;
  175.         iflag++;
  176.     }
  177.  
  178.     bzero((char *)&session, sizeof(struct snmp_session));
  179.     session.peername = host;
  180.     session.community = (u_char *)community;
  181.     session.community_len = strlen((char *)community);
  182.     session.retries = SNMP_DEFAULT_RETRIES;
  183.     session.timeout = SNMP_DEFAULT_TIMEOUT;
  184.     session.authenticator = NULL;
  185.     snmp_synch_setup(&session);
  186.     Session = snmp_open(&session);
  187.     if (Session == NULL){
  188.         printf("Couldn't open snmp\n");
  189.         exit(-1);
  190.     }
  191.     if (pflag) {
  192.         if (tp->pr_stats)
  193.             (*tp->pr_stats)();
  194.         else
  195.             printf("%s: no stats routine\n", tp->pr_name);
  196.         exit(0);
  197.     }
  198.     /*
  199.      * Keep file descriptors open to avoid overhead
  200.      * of open/close on each call to get* routines.
  201.      */
  202.     sethostent(1);
  203.     setnetent(1);
  204.     if (iflag) {
  205.         intpr(interval);
  206.         exit(0);
  207.     }
  208.     if (rflag) {
  209.         if (sflag)
  210.             rt_stats();
  211.         else
  212.             routepr();
  213.         exit(0);
  214.     }
  215.  
  216.     setprotoent(1);
  217.     setservent(1);
  218.     while (p = getprotoent()) {
  219.  
  220.         for (tp = protox; tp->pr_name; tp++)
  221.             if (strcmp(tp->pr_name, p->p_name) == 0)
  222.                 break;
  223.         if (tp->pr_name == 0 || tp->pr_wanted == 0)
  224.             continue;
  225.         if (sflag) {
  226.             if (tp->pr_stats)
  227.                 (*tp->pr_stats)();
  228.         } else
  229.             if (tp->pr_cblocks)
  230.                 (*tp->pr_cblocks)();
  231.     }
  232.     endprotoent();
  233.     exit(0);
  234. }
  235.  
  236. char *
  237. plural(n)
  238.     int n;
  239. {
  240.  
  241.     return (n != 1 ? "s" : "");
  242. }
  243.  
  244. /*
  245.  * Find the protox for the given "well-known" name.
  246.  */
  247. struct protox *
  248. knownname(name)
  249.     char *name;
  250. {
  251.     struct protox *tp;
  252.     
  253.     for (tp = protox; tp->pr_name; tp++)
  254.         if (strcmp(tp->pr_name, name) == 0)
  255.             return(tp);
  256.     return(NULLPROTOX);
  257. }
  258.  
  259. /*
  260.  * Find the protox corresponding to name.
  261.  */
  262. struct protox *
  263. name2protox(name)
  264.     char *name;
  265. {
  266.     struct protox *tp;
  267.     char **alias;            /* alias from p->aliases */
  268.     struct protoent *p;
  269.     
  270.     /*
  271.      * Try to find the name in the list of "well-known" names. If that
  272.      * fails, check if name is an alias for an Internet protocol.
  273.      */
  274.     if (tp = knownname(name))
  275.         return(tp);
  276.         
  277.     setprotoent(1);            /* make protocol lookup cheaper */
  278.     while (p = getprotoent()) {
  279.         /* assert: name not same as p->name */
  280.         for (alias = p->p_aliases; *alias; alias++)
  281.             if (strcmp(name, *alias) == 0) {
  282.                 endprotoent();
  283.                 return(knownname(p->p_name));
  284.             }
  285.     }
  286.     endprotoent();
  287.     return(NULLPROTOX);
  288. }
  289.